(function(){
	$.extend($.fn,{
		regionsSelect : function(value, callback) {
			var $that = $(this);
			//事件：选择地区
			$(document).off("change", "[id^='selectRegion_']");
			$(document).on("change", "[id^='selectRegion_']",function() {
				var type = parseInt(($(this).attr("id").split("_"))[1]);
				$("[id^='selectRegion_']").each(function(i, el) {
					var type2 = parseInt(($(this).attr("id").split("_"))[1]);
					if (type2 > type) {
						$(this).remove();
					}
				});
				if (callback) {
					callback($(this));
				}
				if ($(this).val()) {
					loadByParentId($(this).val());
				}
			});
			
			if (value) {
				loadBrothers(value, true);
			} else {
				loadByParentId(1);
			}
			
			//加载兄弟地区
			function loadBrothers(regionId, isLoadSon){
				// 加载兄弟地区请求地址
				var loadBrothersPath = g_domain + "/region/listBrothers/json";
				
				$.getJSON(loadBrothersPath,{regionId: regionId}, function(data) {
					if (data.status != 0) {
						alert(data.message);
						return;
					}
					var list = data.result;
					if (!list) {
						return;
					}
					var parentId = 0;
					var regionType = 0;
					var $select = null;
					for (var i = 0; i< list.length; i++) {
						var region = list[i];
						var selectId = "selectRegion_" + region.regionType;
						$select = $("#" + selectId);   
						if ($select.length == 0) {
							$that.prepend("<select id='" + selectId + "' class='validate[required]'></select>");
							$select = $("#" + selectId);
						}
						if (i == 0){
							parentId = region.parentId;
							regionType = region.regionType;
							$select.html("");
							$select.append("<option value=''>-请选择-</option>");
						}
						$select.append("<option value='" + region.regionId + "'>" + region.regionName + "</option>");
					}
					$select.val(regionId);
					if (regionType > 0) {
						loadBrothers(parentId, false);
					}
					if (regionId && isLoadSon) {
						loadByParentId(regionId);
					}
					
				});
			}
			
			//加载子一级地区
			function loadByParentId(parentId) {
				//地区列表加载地址
				var loadByParentIdPath = g_domain + "/region/listByParentId/json";

				$.getJSON(loadByParentIdPath, {parentId:parentId}, function(data) {
					if (data.status != 0) {
						alert(data.message);
						return;
					}
					var list = data.result;
					if (!list) {
						return;
					}
					var $select = null;
					for (var i = 0; i< list.length; i++) {
						var region = list[i];
						var selectId = "selectRegion_" + region.regionType;
						$select = $("#" + selectId);   
						if ($select.length == 0) {
							$that.append("<select id='" + selectId + "' class='validate[required]'></select>");
							$select = $("#" + selectId);
						}
						if (i == 0){
							$select.html("");
							$select.append("<option value=''>-请选择-</option>");
						}
						$select.append("<option value='" + region.regionId + "'>" + region.regionName + "</option>");
					}
				});
			}
		}
	});
})();
